<template>
    <div class="email-login">
        <div class="email-login-top border">
            <div class="el-t-left" @click="goToLogin()">&lt; 返回</div>
            <div class="el-t-center">忘记密码</div>
        </div>
        <div class="email-login-content">
            <div class="el-c-top">
                <div class="el-c-t-userid w100 center">
                    <input type="text" class="user_input border" placeholder="邮箱" v-model="userInfo.email">
                </div>
                <div class="el-c-t-password w100 center f-b-r">
                    <input type="text" name="" class="user_input border" placeholder="验证码" v-model="userInfo.verificationCode">
                    <button class="btn" v-if="!isGetVCode" @click="getVerificationCode()">获取验证码</button>
                    <button class="btn" v-else>{{ time }}s</button>
                </div>
                <button class="btn mt" @click="loginByEmail()">下一步</button>
            </div>
        </div>
    </div>
    <error-alert :message="errorMessage" ref="showErrorAlert"></error-alert>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue"
import { useRouter } from "vue-router"
import { IUserIdAndPwdModel } from "../../user.model"
import overallStore from "@/store/index"
import emailLoginStore from "../../email-login/email-login.store"
import { encode } from "@/utils/base64"
// import ErrorAlert from '@/components/error-alert.vue'

const overallSe = overallStore()
const emailLoginSe = emailLoginStore()
const router = useRouter()
const goToLogin = () => {
    router.replace({
        name: 'userIdLogin'
    })
}

const userInfo = reactive({
    email: '',
    verificationCode: ''
}) as IUserIdAndPwdModel

let errorMessage = ref<string>('')
let showErrorAlert = ref<any>(null)
// 获取验证码
const getVerificationCode = () => {
    if(userInfo.email === '') {
        errorMessage.value = '邮箱不能为空！'
        showErrorAlert.value.showErrorAlert()
        return
    }

    let regx = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/i
    if(!regx.test(userInfo.email)) {
        errorMessage.value = '请输入正确的邮箱地址！'
        showErrorAlert.value.showErrorAlert()
        return
    }

    const data = {
        u_email: userInfo.email,
        method: 'login'
    }
    overallSe.getVerificationCodeByEmail(data).then((res: any) => {
        countDown()
        errorMessage.value = '验证码已发送至您的邮箱，请注意查收！'
        showErrorAlert.value.showErrorAlert()
    })
}

// 点击登录按钮
let isShowUnUseTip = ref<boolean>(false)
const loginByEmail = () => {
    if(userInfo.email === '' || userInfo.verificationCode === '') {
        errorMessage.value = '邮箱或验证码不能为空！'
        showErrorAlert.value.showErrorAlert()
        return
    }
    let regx = /^\d+$/i
    if(userInfo.verificationCode.length !== 6 || !regx.test(userInfo.verificationCode)){
        errorMessage.value = '验证码不正确，请重新输入！'
        showErrorAlert.value.showErrorAlert()
        return
    }

    const data = {
        u_email: userInfo.email,
        verification_code: userInfo.verificationCode
    }
    overallSe.getVerificationCodeExpire(data).then((res: any) => {
        if(res.errCode && res.errCode !== '') {
            errorMessage.value = res.errMessage
            showErrorAlert.value.showErrorAlert()
            return 
        } else {
            router.replace({
                name: 'userForgotPasswordReset',
                params: {
                    userEmail: encode(userInfo.email)
                }
            })
        }
    })
}

// 60秒倒计时
let time = ref<number>(60)
let timer = ref<any>()
let isGetVCode = ref<boolean>(false)
const countDown = () => {
    // console.log('timer.value', timer.value)
    // if(timer.value) return
    isGetVCode.value = true
    timer.value = setInterval(() => {
        time.value--
        if(time.value < 0) {
            time.value = 60
            isGetVCode.value = false
            clearInterval(timer.value)
        }
    }, 1000)
}
</script>

<style lang="less" scoped>
.email-login {
    width: 100%;
    height: 100vh;
    position: relative;

    .email-login-top {
        width: 100%;
        height: var(--navigation-bar-height);
        line-height: var(--navigation-bar-height);
        border-bottom: 1px solid #BABABA;
        padding: 0 1.7rem;
        text-align: center;

        .el-t-left {
            color: #1780F1;
            position: absolute;
        }

        .el-t-center {
            color: #000000;
            font-weight: 600;
        }
    }

    .email-login-content {
        width: 100%;
        min-height: calc(100% - 2.5rem);

        .el-c-top {
            height: 21rem;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            padding: 0 3.7rem;

            .el-c-t-agreement{
                font-size: 14px;
                margin-top: 1rem;

                a {
                    color: #000000;
                }

                .el-c-t_checkbox {
                    margin-right: 0.7rem;
                    vertical-align: middle;
                }
            }
        }
    }
}

:deep(.van-dialog) {
    .van-dialog__header {
        padding: 1rem 0;
        font-size: 16px;
        color: var(--font-color-black);
    }
    .van-dialog__content {
        padding: 1rem;
        color: var(--font-color-black);
    }
    .van-dialog__footer {
        .van-button {
            color: var(--font-color-black);
        }
    }
}
</style>